﻿using System;
using System.Data;
using System.Text;

using BLL;
using Model;
using Utility;

namespace EvaluationSystem.Enterprise
{
    public partial class RptSetings : System.Web.UI.Page
    {
        private string m_actid = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((SiteEnterpriseMaster)Master).InitMenu();
            ((SiteEnterpriseMaster)Master).li1.Attributes.Add("class", "hover ccc");
            ((SiteEnterpriseMaster)Master).b_hmenu1.Style.Clear();
            ((SiteEnterpriseMaster)Master).b_hmenu2.Style.Add("display", "none");
            ((SiteEnterpriseMaster)Master).b_hmenu3.Style.Add("display", "none");
            ((SiteEnterpriseMaster)Master).b_hmenu4.Style.Add("display", "none");
            ((SiteEnterpriseMaster)Master).s_menu1_1.Style.Add("COLOR", "#147ab8");
            ((SiteEnterpriseMaster)Master).s_menu1_1.Style.Add("TEXT-DECORATION", "none");   

            if (Session["username"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "<script>alert('请先登录系统!');top.location.href='../Enterprise/EnterpriseLogin.aspx';</script>");
                return;
            }
            else
            {
                if (!IsPostBack)
                {
                    if (Request["actid"] != null)
                    {
                        m_actid = Request["actid"].ToString();

                        ActivityBLL activityBll = new ActivityBLL();
                        EnterpriseUserBLL enterpriseUserBll = new EnterpriseUserBLL();
                        string username = Session["username"].ToString();
                        string enterpriseId = enterpriseUserBll.GetEnterpriseIdByUser(username);
                        DataSet dsActivity = activityBll.GetActivityAll(enterpriseId);
                        if (dsActivity != null)
                        {
                            ddlActivity.DataSource = dsActivity.Tables[0].DefaultView;
                            ddlActivity.DataValueField = "activity_id";
                            ddlActivity.DataTextField = "activity_name";
                            ddlActivity.DataBind();
                            ddlActivity.SelectedValue = m_actid;
                        }
                        bind(Int32.Parse(m_actid));
                    }
                }
            }
        }

        private void bind(int aID)
        {            
            ActivityBLL activityBll = new ActivityBLL();     
            Activity activity = activityBll.GetModel(aID);
            int nPartFlag = activity.Activity_viewreport;

            if (nPartFlag.ToString().Length != 7)
            {
                return;
            }
            else
            {
                if (nPartFlag.ToString().Substring(0, 1).Equals("1"))
                {
                    RadioButtonList1.SelectedValue = "是";
                }
                else
                {
                    RadioButtonList1.SelectedValue = "否";
                }
                //chkViewable.Checked = (nPartFlag.ToString().Substring(0, 1).Equals("1"));
                CheckBox1.Checked = (nPartFlag.ToString().Substring(1, 1).Equals("1"));
                CheckBox2.Checked = (nPartFlag.ToString().Substring(2, 1).Equals("1"));
                CheckBox3.Checked = (nPartFlag.ToString().Substring(3, 1).Equals("1"));
                CheckBox4.Checked = (nPartFlag.ToString().Substring(4, 1).Equals("1"));
                CheckBox5.Checked = (nPartFlag.ToString().Substring(5, 1).Equals("1"));
                CheckBox6.Checked = (nPartFlag.ToString().Substring(6, 1).Equals("1"));
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ActivityBLL activityBll = new ActivityBLL();

            StringBuilder sb = new StringBuilder(7);

            if (RadioButtonList1.SelectedItem.Text.Equals("是"))
            {
                sb.Append("1");
            }
            else
            {
                sb.Append("2");
            }
            sb.Append(CheckBox1.Checked ? "1" : "2");
            sb.Append(CheckBox2.Checked ? "1" : "2");
            sb.Append(CheckBox3.Checked ? "1" : "2");
            sb.Append(CheckBox4.Checked ? "1" : "2");
            sb.Append(CheckBox5.Checked ? "1" : "2");
            sb.Append(CheckBox6.Checked ? "1" : "2");

            string aID = ddlActivity.SelectedValue;
            int rptSettings = int.Parse(sb.ToString());
            int rows = activityBll.UpdateActivityRptSettings(aID, rptSettings);
            if (rows == 1)
            {
                MessageBox.ShowAndRedirect(this, "测试报告设置成功", "ManageActivity.aspx");
            }
            else
            {
                MessageBox.ShowAndRedirect(this, "测试报告设置失败", "ManageActivity.aspx");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ManageActivity.aspx");
        }

        protected void ddlActivity_SelectedIndexChanged(object sender, EventArgs e)
        {
            bind(int.Parse(ddlActivity.SelectedItem.Value));
        }

       
    }
}